namespace RushMail.Requests;

/// <summary>
/// 域名相关接口
/// </summary>
public class Report
{
    private readonly IRushMailClient _client;

    public Report(IRushMailClient client)
    {
        _client = client;
    }

    /// <summary>
    /// 发送任务统计数据接⼝
    /// http://edmapi.rushmail.com/api.php?module=report&action=task-list
    /// </summary>
    /// <returns></returns>
    public async Task<RushMailResponse<ReportTaskListResponse>> TaskList(DateOnly date) =>
        (await _client.PostAsync(new RushMailRequest("report", "task-list")
        {
            Body = new Dictionary<string, string>(){ {"date", date.ToString("yyyy-mm-dd")} }
        })).ToGeneric<ReportTaskListResponse>();
    
    /// <summary>
    /// 发送任务统计数据接⼝
    /// http://edmapi.rushmail.com/api.php?module=report&action=task-list
    /// </summary>
    /// <returns></returns>
    public async Task<RushMailResponse<ReportTaskListResponse>> TaskList(DateOnly dateBegin, DateOnly dateEnd) =>
        (await _client.PostAsync(new RushMailRequest("report", "task-list")
        {
            Body = new Dictionary<string, string>(){ {"date_beg", dateBegin.ToString("yyyy-mm-dd")}, {"date_end", dateEnd.ToString("yyyy-mm-dd")} }
        })).ToGeneric<ReportTaskListResponse>();
    
    /// <summary>
    /// 发送任务统计数据接⼝
    /// http://edmapi.rushmail.com/api.php?module=report&action=task-list
    /// </summary>
    /// <returns></returns>
    public async Task<RushMailResponse<ReportTaskListResponse>> TaskList(string alias) =>
        (await _client.PostAsync(new RushMailRequest("report", "task-list")
        {
            Body = new Dictionary<string, string>(){ {"alias", alias} }
        })).ToGeneric<ReportTaskListResponse>();


    /// <summary>
    /// 发送任务失败详情接⼝
    /// http://edmapi.rushmail.com/api.php?module=report&action=taskfailure-detail
    /// </summary>
    /// <param name="date">指定失败数据的统计⽇期，格式为“2015-06-24”（可选参数）</param>
    /// <param name="type">指定失败的类型； “1”为地址⽆效， “2”为空间不⾜， “3”为对⽅拒收（可选 参数，）</param>
    /// <param name="taskId">要获取失败详情的任务 ID</param>
    /// <param name="alias">单个任务的别名，wctask_id 和 alias 二者选一即可，使⽤此参数时可以省略 “task_id”参数以获得指定任务的完整失败数据（跨天任务处理）（可选参数）</param>
    /// <param name="wctaskId">wctask_id 和 alias 二者选一即可，使⽤此参数时可以省略“task_id”参数以 获得指定任务的完 整失败数据（跨天任务处理）（可选参数）</param>
    /// <returns></returns>
    public async Task<RushMailResponse<int>> TaskfailureDetail(int taskId = 0, string alias = "", int wctaskId = 0, DateOnly? date = null, int type = 0) =>
        (await _client.PostAsync(new RushMailRequest("report", "taskfailure-detail")
        {
            Body = new Dictionary<string, string>()
            {
                {"task_id", taskId.ToString()},
                {"alias", alias},
                {"wctask_id", wctaskId.ToString()},
                {"date", date?.ToString("yyyy-mm-dd")},
                {"type", type.ToString()},
            }
        }))
        .ToGeneric<int>();
    
    /// <summary>
    /// 发件⼈统计数据接⼝（旧）
    /// http://edmapi.rushmail.com/api.php?module=report&action=sender-list
    /// </summary>
    /// <returns></returns>
    public Task<RushMailResponse> SenderList(DateOnly date, string sender = "", int domainId = 0) =>
        _client.PostAsync(new RushMailRequest("report", "sender-list")
        {
            Body = new Dictionary<string, string>()
            {
                {"date", date.ToString("yyyy-mm-dd")}
            }
        });
    
    /// <summary>
    /// 发件⼈统计数据接⼝（旧）
    /// http://edmapi.rushmail.com/api.php?module=report&action=sender-list
    /// </summary>
    /// <returns></returns>
    public Task<RushMailResponse> SenderList(DateOnly dateBegin, DateOnly dateEnd, string sender = "", int domainId = 0) =>
        _client.PostAsync(new RushMailRequest("report", "sender-list")
        {
            Body = new Dictionary<string, string>()
            {
                {"date_beg", dateBegin.ToString("yyyy-mm-dd")},
                {"date_end", dateEnd.ToString("yyyy-mm-dd")},
            }
        });

    /// <summary>
    /// 发件⼈统计数据接⼝-分页返回数据（新）
    /// http://edmapi.rushmail.com/api.php?module=report&action=sender-list-page
    /// </summary>
    /// <returns></returns>
    public Task<RushMailResponse> SenderListPage(DateOnly date, string sender = "", int domainId = 0) =>
        _client.PostAsync(new RushMailRequest("domain", "delete")
        {
            Body = new Dictionary<string, string>() { {"domain_id", domainId.ToString()} }
        });
    
    /// <summary>
    /// 发件⼈统计数据接⼝-分页返回数据（新）
    /// http://edmapi.rushmail.com/api.php?module=report&action=sender-list-page
    /// </summary>
    /// <returns></returns>
    public Task<RushMailResponse> SenderListPage(DateOnly dateBegin, DateOnly dateEnd, string sender = "", int domainId = 0) =>
        _client.PostAsync(new RushMailRequest("domain", "delete")
        {
            Body = new Dictionary<string, string>() { {"domain_id", domainId.ToString()} }
        });
    
    
}


public class ReportTaskListResponse
{
    [JsonProperty("task_list")]public List<ReportTaskItem> TaskList { get; set; }
}

public class ReportTaskItem
{
    [JsonProperty("task_id")]public int TaskId { get; set; }
    [JsonProperty("wctask_id")]public int WctaskId { get; set; }
    public DateTime Date { get; set; }
    public string Alias { get; set; }
    public int Amount { get; set; }
    [JsonProperty("receive_count")]public int ReceiveCount { get; set; }
    [JsonProperty("process_count")]public int ProcessCount { get; set; }
    [JsonProperty("failure_total")]public int FailureTotal { get; set; }
    /// <summary>
    /// 地址⽆效的邮箱数量
    /// </summary>
    [JsonProperty("failure1_count")]public int Failure1Count { get; set; }
    /// <summary>
    /// 空间不⾜的邮箱数量
    /// </summary>
    [JsonProperty("failure2_count")]public int Failure2Count { get; set; }
    /// <summary>
    /// 对⽅拒收的邮箱数量
    /// </summary>
    [JsonProperty("failure3_count")]public int Failure3Count { get; set; }
}
